﻿
var tabPanelContainerHelper = function (jo, settings) {
    this.jo = jo;
    this.settings = settings;
    var _this = this;
    this.nav = null;
    this.content = null;
    this.navitemwarp = null;
    this.tabList = new List();
    this.init = function () {
        this.jo.addClass("tabpanel");
        this.nav = this.jo.children(this.settings.navSlt);
        this.content = this.jo.children(this.settings.contentSlt);
        this.nav.addClass("header");
        this.content.addClass("content");
        this.nav.html("<div class=\"left\"></div><div class=\"nav\"><ul></ul></div><div class=\"right\"></div>");
        // this.nav.horizontalSplitContainer();
        //this.jo.verticalSplitContainer();
        this.navitemwarp = this.nav.children(".nav").children("ul");
        this.nav.children(".left").click(function () {
            _this.nav.children(".nav").animate({ scrollLeft: _this.nav.children(".nav").scrollLeft() - 100 });
        });
        this.nav.children(".right").click(function () {
            _this.nav.children(".nav").animate({ scrollLeft: _this.nav.children(".nav").scrollLeft() + 100 });
        });
        this.jo.layout(function () {
            _this.layout();
        });
        this.layout();
        $(window).resize(function () {
            _this.layout();
        });
    };
    this.addTab = function (tabarg) {
        var index = this.tabList.exists(function (item) { return item.name == tabarg.name; });

        if (index !== false) {
            this.selectTab(this.tabList.get(index).id);
            //this.refreshTab(this.tabList.get(index).id)
            //this.removeTab(this.tabList.get(index).id);
            return;
        }
        var content = $("<div style='height:100%;width:100%;overflow:hidden'><div id='_hyt_loading' style='width:100%;text-align:center;'><img src='/Resources/project/images/loading.gif'/></div></div>");
        this.content.append(content);
        var id = "tab" + new Date().getTime();
        content.attr("id", id);
        if (tabarg.html) {
            content.html(tabarg.html);
        } else if (tabarg.url) {
            Utils.loadUrlto(tabarg.url, content, tabarg.frompointerid, tabarg.intent, function (iframe) {
                content.find("#_hyt_loading").remove();
                iframe.contentWindow.mWindowPointer.listen(intent.action.closewindow, function (_pointer, _arg) {
                    _this.removeTab(id);
                });
            });
            //content.html('<iframe style="width:100%;height:100%" frameborder="0" src="' + tabarg.url + '"></iframe>');
        }

        var html = '<li tabid="{id}"><a title="{title}" href="javascript:"><span>{title}</span></a><a class="close" href="javascript:;">close</a></li>';
        html = html.replace(/\{id\}/g, id)
                       .replace(/\{title\}/g, tabarg.name);
        var navjo = $(html);
        tabarg = $.extend({ closeable: true }, tabarg);
        if (tabarg.closeable) {
            navjo.children(".close").click(function () {
                _this.removeTab(content.attr("id"));
                return false;
            });
        } else {
            navjo.children(".close").hide();
        }
        navjo.click(function () {
            _this.selectTab(content.attr("id"));
            return false;
        });
        this.navitemwarp.append(navjo);
        this.tabList.push($.extend({ id: content.attr("id"), nav: navjo, content: content, selected: false, frompointerid: null, intent: null }, tabarg));
        this.selectTab(content.attr("id"));
        this.layout();
        this.selectTab(content.attr("id"));
        return id;
    };
    this.removeTab = function (tabid) {
        var o = this.tabList.first(function (item) { return item.id == tabid; });
        if (o.url) {
            try {
                var iframe = o.content.find("iframe").get(0);
                var p = mPointerManager.get(iframe.contentWindowPointerid);
                if (p) {
                    p.release();
                }
                //mPointerManager.remove(iframe.contentWindowPointerid);
            } catch (ex) { }
        }
        o.content.remove();
        o.nav.remove();
        this.tabList.remove(function (item) { return item.id == tabid; });
        var length = this.tabList.length();
        var newo = this.tabList.get(length - 1);
        if (newo != null) {
            this.selectTab(newo.id);
        }
        this.layout();
    };
    this.selectTab = function (tabid) {
        var oldo = this.tabList.first(function (item) { return item.selected });
        if (oldo != null) {
            oldo.content.hide();
            oldo.nav.removeClass("selected");
            oldo.selected = false;

        }
        var o = this.tabList.first(function (item) { return item.id == tabid; });
        var index = this.tabList.getIndex(function (item) { return item.id == tabid; });
        o.content.show();
        o.nav.addClass("selected");
        o.nav.find("a").blur();

        if (o.url) {
            try {
                var iframe = o.content.find("iframe").get(0);
                iframe.contentWindow.mWindowPointer.notify(intent.action.focus);
            } catch (ee) {

            }
            // var fromcomponentid = oldo.content.find("iframe").get(0).contentWindow.mWindowComponent
            // var fromcomponent = mComponentManager.get(oldo.fromcomponentid);
            // if (fromcomponent) {
            //     fromcomponent.focus();
            // }
        }
        var scrollleft = this.nav.children(".nav").scrollLeft();
        var oleft = o.nav.position().left;

        if (scrollleft > oleft) {
            this.nav.children(".nav").animate({ scrollLeft: oleft });
        } else if (oleft + o.nav.outerWidth() > scrollleft + this.nav.children(".nav").width()) {
            this.nav.children(".nav").animate({ scrollLeft: oleft - this.nav.children(".nav").width() + o.nav.outerWidth() });
        }
        o.selected = true;
        this.tabList.moveToTop(index);
        this.jo.layout();
    };
    this.layout = function () {
        var items = this.navitemwarp.children();
        var w = 0;
        items.each(function () {
            w += $(this).outerWidth();
        });
        this.navitemwarp.width(w + 100);
        if (w > this.nav.width()) {
            this._showleftright();
        } else {
            this._hideleftright();
        }
    };
    this._hideleftright = function () {
        this.nav.children(".left,.right").width(0).hide();
        this.nav.children(".nav").width(this.nav.width());
        //this.nav.helper("horizontalSplitContainer").layout();
    };
    this._showleftright = function () {
        this.nav.children(".left,.right").width(17).show();
        this.nav.children(".nav").width(this.nav.width() - 34);
        //this.nav.helper("horizontalSplitContainer").layout();
    };
};
if (self == top) {
    $(function () {
        window.mTabPanelContainerHelper = new tabPanelContainerHelper($("#desktop_content"), {
            navSlt: "#desktop_navbar",
            contentSlt: "#desktop_iframes",
            maxitem: 0
        });
        mTabPanelContainerHelper.init();
    });
} else {
    window.mTabPanelContainerHelper = top.window.mTabPanelContainerHelper;
}

(function ($) {
    $.fn.tabPanelContainer = function (option) {
        option = $.extend({
            navSlt: "#desktop_navbar",
            contentSlt: "#desktop_iframes",
            maxitem: 0
        }, option);
        return this.each(function () {
            var view = $(this);
            var settings = $.extend({}, option);
            var helper = new tabPanelContainerHelper(view, settings);
            helper.init();
            view.helper("tabPanelContainer", helper);
        });
    };
})(jQuery);